//
//  AddCategoryAction.swift
//  Demo_localRemoteNotification
//
//  Created by 李煜 on 2023/9/26.
//

import UIKit

let NOTIFICATION_REQUEST1 = "REQUEST"
let id_LocalNotificationRequest = "localNotificationID"

class AddCategoryAction: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let generalCategory = UNNotificationCategory(identifier: "GENERAL",
                                                     actions: [], intentIdentifiers: [], options: .customDismissAction)
        let snoozeAction = UNNotificationAction(identifier: "SNOOZE_ACTION", title: "snooze", options: UNNotificationActionOptions(rawValue: 0))
        let stopAction = UNNotificationAction(identifier: "STOP_ACTION", title: "Stop", options: .foreground)
        let expiredCategory = UNNotificationCategory(identifier: "TIMER_EXPIRED", actions: [snoozeAction, stopAction], intentIdentifiers: [], options: UNNotificationCategoryOptions(rawValue: 0))
        let center = UNUserNotificationCenter.current()
        center.setNotificationCategories([generalCategory, expiredCategory])

        // 获取通知中心的通知列表
        center.getDeliveredNotifications { list in
            for item in list {
                print(item)
            }
        }

        // 删除过期通知方法
        let request = UNNotificationRequest(identifier: NOTIFICATION_REQUEST1, content: UNNotificationContent(), trigger: nil)
        center.removeDeliveredNotifications(withIdentifiers: [NOTIFICATION_REQUEST1])
    }

    func makeLocalNotification() {
        // 本地通知
        let content = UNMutableNotificationContent()
        content.title = NSString.localizedUserNotificationString(forKey: "Wake up!", arguments: nil)
        content.body = NSString.localizedUserNotificationString(forKey: "Rise and shine! It's morning time!",
                                                                arguments: nil)

        // Configure the trigger for a 7am wakeup.
        var dateInfo = DateComponents()
        dateInfo.hour = 7
        dateInfo.minute = 0
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: false)

        // Create the request object.
        let request = UNNotificationRequest(identifier: "MorningAlarm", content: content, trigger: trigger)
    }
    func schedulingLocalNotificationDelivery() {
        // 本地通知
        let content = UNMutableNotificationContent()
        content.title = NSString.localizedUserNotificationString(forKey: "Wake up!", arguments: nil)
        content.body = NSString.localizedUserNotificationString(forKey: "Rise and shine! It's morning time!",
                                                                arguments: nil)

        // Configure the trigger for a 7am wakeup.
        var dateInfo = DateComponents()
        dateInfo.hour = 7
        dateInfo.minute = 0
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: false)

        // Create the request object.
        let request = UNNotificationRequest(identifier: "MorningAlarm", content: content, trigger: trigger)
        // Schedule the request.
        let center = UNUserNotificationCenter.current()
        center.add(request) { (error : Error?) in
            if let theError = error {
                print(theError.localizedDescription)
            }
        }
    }
}

